package com.pai4j.user.repository.dao;

import com.pai4j.user.repository.entity.GroupJoinApplicationEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 加群申请数据访问接口
 */
@Repository
public interface IGroupJoinApplicationDAO extends JpaRepository<GroupJoinApplicationEntity, Long> {

    /**
     * 根据群组ID和申请人账号查找申请记录
     */
    GroupJoinApplicationEntity findByGroupPaiIdAndApplicantAccount(String groupPaiId, String applicantAccount);

    /**
     * 根据ID和状态查找申请记录
     */
    GroupJoinApplicationEntity findByIdAndApplicationStatus(Long id, Integer applicationStatus);

    /**
     * 查询群组的待处理申请列表（分页）
     */
    Page<GroupJoinApplicationEntity> findByGroupPaiIdAndApplicationStatusOrderByCreateDateDesc(
            String groupPaiId, Integer applicationStatus, Pageable pageable);

    /**
     * 查询用户的申请历史（分页）
     */
    Page<GroupJoinApplicationEntity> findByApplicantAccountOrderByCreateDateDesc(
            String applicantAccount, Pageable pageable);

    /**
     * 统计群组的待处理申请数量
     */
    Long countByGroupPaiIdAndApplicationStatus(String groupPaiId, Integer applicationStatus);

    /**
     * 统计用户在某个群的待处理申请数量
     */
    Long countByGroupPaiIdAndApplicantAccountAndApplicationStatus(
            String groupPaiId, String applicantAccount, Integer applicationStatus);

    /**
     * 批量更新过期的申请状态
     */
    @Query("UPDATE GroupJoinApplicationEntity a SET a.applicationStatus = :expiredStatus, a.updateDate = :updateDate " +
           "WHERE a.applicationStatus = :pendingStatus AND a.expireTime < :now")
    int updateExpiredApplications(@Param("pendingStatus") Integer pendingStatus,
                                   @Param("expiredStatus") Integer expiredStatus,
                                   @Param("now") Date now,
                                   @Param("updateDate") Date updateDate);

    /**
     * 查询某个群组的所有待处理申请（不分页，用于缓存）
     */
    List<GroupJoinApplicationEntity> findByGroupPaiIdAndApplicationStatus(String groupPaiId, Integer applicationStatus);

    /**
     * 查询群组的未读待审核申请列表
     * 按创建时间倒序排列
     *
     * @param groupPaiId 群组ID
     * @param applicationStatus 申请状态（PENDING）
     * @return 未读的待审核申请列表
     */
    @Query("SELECT a FROM GroupJoinApplicationEntity a " +
           "WHERE a.groupPaiId = :groupPaiId " +
           "AND a.applicationStatus = :applicationStatus " +
           "AND a.isRead = false " +
           "ORDER BY a.createDate DESC")
    List<GroupJoinApplicationEntity> findUnreadApplications(@Param("groupPaiId") String groupPaiId,
                                                             @Param("applicationStatus") Integer applicationStatus);

    /**
     * 批量标记群组的待审核申请为已读
     *
     * @param groupPaiId 群组ID
     * @param applicationStatus 申请状态（PENDING）
     * @return 更新的记录数
     */
    @Query("UPDATE GroupJoinApplicationEntity a SET a.isRead = true " +
           "WHERE a.groupPaiId = :groupPaiId " +
           "AND a.applicationStatus = :applicationStatus " +
           "AND a.isRead = false")
    @org.springframework.data.jpa.repository.Modifying
    @org.springframework.transaction.annotation.Transactional
    int markApplicationsAsRead(@Param("groupPaiId") String groupPaiId,
                                @Param("applicationStatus") Integer applicationStatus);

    /**
     * 统计群组未读的待审核申请数量
     *
     * @param groupPaiId 群组ID
     * @param applicationStatus 申请状态（PENDING）
     * @return 未读待审核数量
     */
    @Query("SELECT COUNT(a) FROM GroupJoinApplicationEntity a " +
           "WHERE a.groupPaiId = :groupPaiId " +
           "AND a.applicationStatus = :applicationStatus " +
           "AND a.isRead = false")
    Long countUnreadApplications(@Param("groupPaiId") String groupPaiId,
                                   @Param("applicationStatus") Integer applicationStatus);
}
